<template>
  <van-popup
    :value="value"
    @click-close-icon="$emit('input', false)"
    closeable
    position="left"
  >
  <div class="article-channel">
    <div class="head">
      <h3>我的频道<small>点击进入频道</small></h3>
      <a class="edit" href="javascript:;">编辑</a>
    </div>
    <div class="body">
      <a href="javascript:;"  @click="enterChannel(i.id)" :class="{active:activeIndex===index}" v-for="(i,index) in list" :key="i.id">{{i.name}}</a>
    </div>
    <div class="head" style="margin-top:12px">
      <h3>频道推荐</h3>
    </div>
    <div class="body">
      <a href="javascript:;" v-for="item in optional" :key="item.id" @click="add(item)">+{{item.name}}</a>
    </div>
  </div>
  </van-popup>
</template>
<script>
import { Channels } from '@/api/channel'
export default {
  name: 'ArticleChannel',
  props: {
    value: {
      type: Boolean,
      default: false
    },
    list: {
      type: Array,
      required: true
    },
    activeIndex: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
      allChannels: []
    }
  },
  // 获取所有的频道
  async created () {
    const res = await Channels()
    // console.log(res)
    this.allChannels = res.data.channels
  },
  methods: {
    async enterChannel (index) {
      // 关闭对话框
      this.$emit('input', false)
      // 传递频道索引
      this.$emit('bling', index)
    },
    // 点击添加功能 不会做
    async add (cap) {
      console.log(cap)
      const newChannel = []
      this.list.forEach((item, index) => {
        if (index !== 0) {
          newChannel.push({
            id: item.id,
            name: item.name,
            seq: index
          })
        }
      })

      // const res = await xiuChannel({
      //   channels: this.list,
      //   id: item.id,
      //   seq: item.name
      // })
      // console.log(res)
    }
  },
  computed: {
    // 可选频道
    optional () {
      return this.allChannels.filter(b => !this.list.find(a => a.id === b.id))
    }
  }
}
</script>
<style scoped lang="less">
.van-popup {
  width: 100%;
  height: 100%;
  ::v-deep .van-popup__close-icon {
    font-size: 20px;
    right: 12px;
    top: 12px;
  }
}
.article-channel {
  margin-top: 44px;
  .head {
    padding: 0 16px;
    display: flex;
    justify-content: space-between;
    justify-items: center;
    padding-bottom: 12px;
    h3 {
      font-size: 16px;
      color: #333;
      margin: 0;
      small {
        font-size: 12px;
        color: #999;
        margin-left: 10px;
      }
    }
    .edit {
      float: right;
      height: 22px;
      width: 52px;
      line-height: 22px;
      text-align: center;
      color: #DE644B;
      border-radius: 11px;
      border: 1px solid #DE644B;
      font-size: 12px;
      &.active {
        color: #fff;
        background: #DE644B;
      }
    }
  }
  .body {
    padding: 0 6px 0 16px;
    a {
      display: inline-block;
      padding: 0 8px;
      font-size:14px;
      color: #3A3948;
      background: #F7F8FA;
      height: 36px;
      line-height: 36px;
      min-width: 78px;
      margin-right: 10px;
      margin-bottom: 12px;
      border-radius: 18px;
      text-align: center;
      &.active {
        color: #DE644B;
      }
    }
  }
}
</style>
